package org.szy.plugin.dev.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.core.common.uilts.ReqUtil;
import org.szy.plugin.dev.entity.DevMessage;
import org.szy.plugin.dev.entity.DevMessageUser;
import org.szy.plugin.dev.param.DevMessageUserQueryParam;
import org.szy.plugin.dev.service.DevMessageService;
import org.szy.plugin.dev.service.DevMessageUserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 站内消息用户关联控制器
 *
 * @author wangbuhao
 * @since 2023-06-28 18:17:41
 */
@Controller
@Mapping("/dev/messageUser")
public class DevMessageUserController extends BaseController<DevMessageUser> {

    private final String PATH_PREFIX = "dev/messageUser/";
    @Inject
    private DevMessageUserService devMessageUserService;
    @Inject
    private DevMessageService devMessageService;

    /**
     * 用户消息展示页
     */
    @Get
    @Mapping("/message")
    public ModelAndView message() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "message");
        return view;
    }

    /**
     * 站内消息用户关联列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 站内消息用户关联详情页
     */
    @Get
    @Mapping("/detail")
    public ModelAndView detail() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "detail");
        view.put("messageId", ReqUtil.getStr("messageId"));
        return view;
    }

    /**
     * 获取站内消息用户关联分页数据
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:41
     */
    @Get
    @Mapping("/page")
    public R<Page> page(DevMessageUserQueryParam param) {
        return R.data(devMessageUserService.page(getPage(), param));
    }

    /**
     * 获取站内消息详情
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:41
     */
    @Get
    @Mapping("/getInfo")
    public R<DevMessage> getInfo(@NotNull Long id) {
        DevMessage message = devMessageService.getById(id, "category", "title", "content", "publish_time");
        return R.asNull(message);
    }

    /**
     * 删除站内消息用户关联
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:41
     */
    @Delete
    @Mapping("/delete")
    public R<?> delete(@NotNull Long id) {
        boolean delete = devMessageUserService.delete(id, StpUtil.getLoginId());
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 是否有未读消息
     */
    @Get
    @Mapping("/unReadCount")
    public R<?> unReadCount() {
        long count = devMessageUserService.unRead(StpUtil.getLoginIdAsString());
        Map<String, Object> map = new HashMap<>(1);
        map.put("count", count);
        return R.data(map);
    }

    /**
     * 类型数量组
     *
     * @param messageStatus 状态
     * @return
     */
    @Get
    @Mapping("/categoryCount")
    public R<?> categoryCount(String messageStatus) {
        List<Map> maps = devMessageUserService.categoryCount(messageStatus);
        return R.data(maps);
    }

    /**
     * 阅读消息
     */
    @Post
    @Mapping("/read")
    public R<?> read(@NotBlank String ids) {
        boolean read = devMessageUserService.read(ids, StpUtil.getLoginId());
        return R.ok();
    }
}
